#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <iostream>
#include <thread>

#include <mutex>

#include <condition_variable>

#include "thread_test.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

extern std::mutex some_mutex;

class background_task
{
public:
    explicit background_task()
    {
        canWakeup = false;
    }

    void backWork(int age = 0)
    {
        std::cout << "sub thread id " << std::this_thread::get_id() << " backWork " << age << std::endl;
    }

    void noParam()
    {
        std::cout << "sub thread id " << std::this_thread::get_id() << " noParam " << std::endl;
    }

    void operator()(int age = 0) const
    {
        std::cout << "sub thread id " << std::this_thread::get_id() << " my age " << age << std::endl;
    }

    void lookTest()
    {
        std::cout << "look begin " << std::endl;
//        m_some_mutex.lock();

        std::lock_guard<std::mutex> lock(m_some_mutex);

        std::cout << "look end " << std::endl;

        std::cout << "sub thread id " << std::this_thread::get_id() << " backWork " << std::endl;

//        m_some_mutex.unlock();
    }

    void testCond()
    {
        std::cout << "wait begin " << std::endl;

//        std::unique_lock<std::mutex> ul(m_some_mutex);
//        m_cond.wait(ul, [&](){return canWakeup;});

        while (!canWakeup)
        {
            std::unique_lock<std::mutex> ul(m_some_mutex);
                    m_cond.wait(ul);
        }

        std::cout << "wait end " << std::endl;
    }

    void wakeup()
    {
        std::lock_guard<std::mutex> look(m_some_mutex);

        m_cond.notify_one();
    }

    void wakeup2()
    {
        std::lock_guard<std::mutex> look(m_some_mutex);
        canWakeup = true;
        m_cond.notify_one();
    }

private:
    std::mutex m_some_mutex;

    std::condition_variable m_cond;
    bool canWakeup;
};








class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_6_clicked();

private:
    Ui::MainWindow *ui;


//    std::lock_guard<std::mutex> guard(some_mutex);
    std::thread td1;
    std::thread td2;
    std::thread td3;

    background_task m_backwork;


    Thread_test* plogthread;

};
#endif // MAINWINDOW_H
